/**
 * 考勤填报 Created by luoxiaoming on 15-12-9.
 */
Ext.define("app.ux.attFill", {
    play : function(tabs) {
        var tab = Ext.getCmp('attendanceFill');
        if (!tab) {
            Ext.Ajax.request({
                url : 'user/loadCurrUser',
                success : function(response) {
                    var user = Ext.decode(response.responseText);
                    var s = Ext.Date.format(new Date(), 'Y-m-d');
                    var n = new Date(s.replace(/\-/g, "/"));
                    var p = new Date(n.setMonth(n.getMonth() - 1));
                    if (new Date() < new Date(Ext.Date
                                .format(new Date(), 'Y-m')
                            + "-15"))
                        var y = Ext.Date.format(p, 'Y-m');
                    else
                        var y = Ext.Date.format(new Date(), 'Y-m');

                    Ext.Ajax.request({
                        url : 'attendance/generatorDayOfMonthList',
                        params : {
                            yearMonth : y
                        },
                        success : function(response) {
                            var columns = Ext.decode(response.responseText);
                            columns.unshift({
                                "dataIndex" : "name",
                                "width" : 50,
                                "text" : "姓名",
                                css : 'color:red;'
                            });

                            columns.unshift({
                                "dataIndex" : "staffCode",
                                "text" : "职工代码",
                                hidden : true
                            });

                            var fields = new Array();
                            var combobox = {
                                xtype : 'combobox',
                                editable : false,
                                triggerAction : 'all',
                                store : Ext.data.StoreManager.lookup("attendanceTypeWithUseFlag"),
                                displayField : 'itemName',
                                valueField : 'itemCode'
                            };
                            for (var i = 0; i < columns.length; i++) {
                                if (i < 2) {
                                    fields.push({
                                        name : columns[i]['dataIndex'],
                                        type : 'string'
                                    });
                                } else {
                                    fields.push({
                                        name : columns[i]['columns'][0]['dataIndex'],
                                        type : 'string'
                                    });
                                    var el = columns[i]['columns'][0];
                                    el['editor'] = combobox;
                                    el['renderer'] = function(val) {

                                        var d = Ext.data.StoreManager.lookup("attendanceType").findRecord('itemCode', val,0,false,true,true);

                                        /*
                                         * if (val != "A" && d != null) { return "<span
                                         * style='color:red'>" +
                                         * d.get("itemName") + "</span>"; };
                                         */

                                        if (d != null) {
                                            if (val == 'B13') {
                                                return "<span style='color:red'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B14') {
                                                return "<span style='color:blue'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B3') {
                                                return "<span style='color:green'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B4') {
                                                return "<span style='color:#CD00CD'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B5') {
                                                return "<span style='color:#4020ff'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B6') {
                                                return "<span style='color:#6000ff'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else

                                            if (val == 'B7') {
                                                return "<span style='color:	#844200'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B8') {
                                                return "<span style='color: #FFFF93'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B9') {
                                                return "<span style='color:	#336666'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B10') {
                                                return "<span style='color:#5151A2'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B11') {
                                                return "<span style='color:#64A600'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B12') {
                                                return "<span style='color:#6C3365'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else

                                            if (val == 'B13') {
                                                return "<span style='color:#930000'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B14') {
                                                return "<span style='color:#930093'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B15') {
                                                return "<span style='color:#600000'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B16') {
                                                return "<span style='color:#01B468'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B17') {
                                                return "<span style='color:#FF8040'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else if (val == 'B18') {
                                                return "<span style='color:#9F4D95'>"
                                                    + d.get("itemName")
                                                    + "</span>";
                                            } else

                                            {

                                                return d.get("itemName")


                                            };
                                        };
                                    }
                                    columns[i]['columns'][0] = el;
                                }

                            }

                            columns.unshift({
                                xtype : 'rownumberer',
                                width : 30
                            });
                            var date = Ext.Date.format(new Date(), 'Y-m');

                            var form = Ext.create("app.ux.attendance.form", {
                                height : '12%',
                                split : true,
                                collapsible : true, // 展开 收缩
                                collapsed : false,
                                id : "attform",
                                frame : true,
                                region : "north"
                            });

                            var values = form.getForm().getValues();
                            values['deptCode'] = user.deptCode;
                            values['username'] = user.username;
                            values['type'] = user.type;
                            values['nurse'] = user.nurse;
                            var strDate = Ext.Date.format(new Date(), 'Y-m-d');
                            var now = new Date(strDate.replace(/\-/g, "/"));
                            var perMonth = new Date(now.setMonth(now.getMonth()- 1));
                            if (new Date() < new Date(Ext.Date.format(new Date(), 'Y-m')+ "-15"))
                                values['yearMonth'] = Ext.Date.format(perMonth, 'Y-m');
                            else
                                values['yearMonth'] = Ext.Date.format(new Date(), 'Y-m');
                            form.getForm().setValues(values);

                            Ext.define('temprow', {
                                extend : 'Ext.data.Model',
                                fields : fields
                            });


                            var store = Ext.create("Ext.data.Store", {
                                autoLoad: true,
                                model: 'temprow',
                                proxy: {
                                    type: 'ajax',
                                    url: 'attendance/loadAttendance',
                                    reader: {
                                        type: 'json',
                                        root: 'root',
                                        totalProperty: 'total'
                                    }
                                },
                                listeners: {
                                    beforeload: function (store, options) {
                                        Ext.apply(store.proxy.extraParams, values);
                                    }
                                }
                            });
                            if (user.type == '1') {

                                var tbar=[{
                                    text : '领导签字',
                                    tooltip : '提交数据',
                                    iconCls : 'update',
                                    handler : function() {
                                        Ext .create('app.ux.attendance.leaderCollect', {}) .collect(form .getForm() .getValues());


                                    }
                                },{
                                    text : '退回',
                                    tooltip : '提交数据',
                                    iconCls : 'update',
                                    handler : function() {
                                        var values = form.getForm().getValues();
                                        values.status='0';
                                        Ext.Ajax.request({
                                            url : 'attendance/updateAttendanceStatus',
                                            params : values,
                                            success : function(response) {
                                                Ext.getCmp('attendanceFill')
                                                    .query("grid")[0].store
                                                    .load({params : form.getForm().getValues()});
                                                Ext.Msg.alert("提示","退回成功,请通知考勤员重新填写");
                                            }
                                        });
                                    }
                                }];
                            }
                            else
                            {var tbar = [{
                                text : '填报',
                                tooltip : '填报考勤',
                                iconCls : 'add',
                                handler : function() {
                                    Ext.Msg.confirm('系统提示','填报会清除本月已填报的数据,确定要填报吗？', function(btn) {
                                        if (btn == 'yes') {
                                            var values = form.getForm().getValues();
                                            store.load({
                                                url : 'attendance/attendanceFill',params : values,
                                                callback : function(records,operation,success) {
                                                    var res = Ext
                                                        .decode(operation.response.responseText);
                                                    if (!res.result) {
                                                        Ext.Msg.alert("提示",res.msg);
                                                    } else {
                                                        Ext.getCmp('attendanceFill').query("grid")[0].store.load({params : form.getForm().getValues()
                                                        });
                                                    }
                                                }
                                            })
                                        }
                                    }, this);
                                }
                            }, {
                                text : '刷新',
                                tooltip : '刷新数据',
                                iconCls : 'refresh',
                                handler : function() {
                                    var params = form.getForm().getValues();
                                    this.ownerCt.ownerCt.store.load({
                                        params : params
                                    });
                                    var grid = this.ownerCt.ownerCt;
                                    grid.getSelectionModel().clearSelections()
                                }
                            }, {
                                text : '设置为正常',
                                tooltip : '设置为正常',
                                iconCls : 'reset',
                                handler : function() {
                                    Ext.Ajax.request({
                                        url : 'attendance/setAllOtherProperties',
                                        params : form.getForm().getValues(),
                                        success : function(response) {
                                            Ext.getCmp('attendanceFill')
                                                .query("grid")[0].store
                                                .load({params : form.getForm().getValues()});
                                        }
                                    });
                                }
                            }, {
                                text : '查看填报情况',
                                tooltip : '查看填报情况',
                                iconCls : 'update',
                                handler : function() {
                                    var form = this.ownerCt.ownerCt.ownerCt.query('form')[0].getForm().getValues();

                                    Ext.create('app.ux.attendance.collect', {}).collect(form);

                                }
                            },{
                                text : '提交',
                                tooltip : '提交数据',
                                iconCls : 'update',
                                handler : function() {
                                    Ext.Ajax.request({
                                        url : 'attendance/loadNullName',
                                        params : form.getForm().getValues(),
                                        success : function(response) {
                                            var text = response.responseText;
                                            var res = Ext.decode(text)
                                            if (res.success)
                                                Ext .create('app.ux.attendance.collect', {}) .collect(form .getForm() .getValues());
                                            else if (res.failure)
                                                Ext.Msg.alert('提示', res.failure);
                                        },
                                        failure : function(response) {
                                            var text = response.responseText;
                                            var res = Ext.decode(text)
                                            if (res.success)
                                                Ext .create( 'app.ux.attendance.collect', 	{}) .collect(form .getForm() .getValues());
                                            else if (res.failure)
                                                Ext.Msg.alert('提示',res.failure);
                                        }
                                    });

                                }
                            },
                                {
                                    text : '增加考勤人员',
                                    tooltip : '增加人员',
                                    iconCls : 'add',
                                    handler : function() {Ext.create('app.ux.attendance.vstaffAdd',{}).edit(form .getForm() .getValues());
                                    }
                                }/*, {
                                 text : '模板',
                                 tooltip : '模板',
                                 iconCls : 'update',
                                 handler : function() {Ext.create('app.ux.attendance.attModel',{}).edit();
                                 }
                                 }*/];
                                if (user.type == '2')
                                    tbar.push({text : '领导签字',
                                        tooltip : '提交数据',
                                        iconCls : 'update',
                                        handler : function() {
                                            Ext .create('app.ux.attendance.leaderCollect', {}) .collect(form .getForm() .getValues());


                                        }});

                            }

                            var grid = Ext.create("app.ux.attendance.grid", {
                                id : 'attFill',
                                region : "center",
                                title : '考勤上报',
                                columns : columns,
                                store : store,
                                selType : 'cellmodel',
                                tbar : tbar
                            });
                            tab = tabs.add({
                                xtype : 'panel',
                                id : 'attendanceFill',
                                title : '考勤填报',
                                closable : true,
                                layout : "border",
                                items : [form, grid]
                            });
                            tabs.setActiveTab(tab);
                        }
                    })

                }
            })

        }else{
            tabs.setActiveTab(tab);
        }


    }
})